<script setup>
import { useUserStore } from '@/store/modules/user'
import { Phone, Lock } from '@element-plus/icons-vue'
import SendMessageBtn from './SendMessageBtn.vue'
import { PHONE_REGEXP } from '@/utils/regExp'
import { sm4Encrypt } from '@/utils/sm4'
import { phoneLogin, getRedirectUrl, getLoginTicket } from '@/api/system/app'

// 获取router变量
const vueRouter = useRouter()
// 获取store变量
const userStore = useUserStore()

const formRef = ref()

// 定义表单对象
const loginFormModel = reactive({
  userName: '',
  code: '',
  uuid: '',
  codeUrl: '',
  loginButtonDisabled: false,
  loginButtonLoading: false,
  loginButtonName: '登录'
})
// 定义表单校验规则
const formRules = reactive({
  userName: [
    { required: true, message: '请输入手机号！', trigger: 'blur' },
    { pattern: PHONE_REGEXP, trigger: 'blur', message: '请输入正确的手机号码' }
  ],
  code: [{ required: true, message: '请输入短信验证码！', trigger: 'blur' }]
})

const validate = () => {
  formRef.value.validateField(['userName'])
}

const ticket = ref()
const vueRoute = useRoute()
// 提交表单方法
const submitForm = () => {
  loginFormModel.loginButtonDisabled = true
  loginFormModel.loginButtonLoading = true
  loginFormModel.loginButtonName = '登录中...'
  formRef.value.validate((valid) => {
    if (valid) {
      phoneLogin({
        userName: loginFormModel.userName,
        code: loginFormModel.code,
      })
        .then(res => {
          getRedirectUrl(ticket.value || vueRoute.query.ticket, res.data.token).then(result => {
            const url = new URL(result.data.redirectUrl)
            if (url.search) {
              window.location.href = url + '&access_token=' + res.data.token
            } else {
              window.location.href = url + '?access_token=' + res.data.token
            }
          }).catch(() => {
            getLoginTicket().then(res => {
              ticket.value = res.data.ticket
            })
            loginFormModel.loginButtonDisabled = false
            loginFormModel.loginButtonLoading = false
            loginFormModel.loginButtonName = '登录'
          })
        })
        .catch(() => {
          loginFormModel.loginButtonDisabled = false
          loginFormModel.loginButtonLoading = false
          loginFormModel.loginButtonName = '登录'
        })
    } else {
      loginFormModel.loginButtonDisabled = false
      loginFormModel.loginButtonLoading = false
      loginFormModel.loginButtonName = '登录'
    }
  })
}

</script>

<template>
  <el-form ref="formRef" :model="loginFormModel" :rules="formRules">
    <el-form-item prop="userName">
      <el-input v-model="loginFormModel.userName" :prefix-icon="Phone" placeholder="请输入手机号" clearable />
    </el-form-item>
    <el-form-item prop="code">
      <div class="flex flex-grow items-center">
        <el-input
          v-model="loginFormModel.code"
          type="code"
          :prefix-icon="Lock"
          placeholder="请输入短信验证码"
          clearable
        />
        <SendMessageBtn :userName="loginFormModel.userName" @validate="validate"/>
      </div>
    </el-form-item>
    <div class="flex justify-center mt-6">
      <el-button
        type="primary"
        @click="submitForm"
        :disabled="loginFormModel.loginButtonDisabled"
        :loading="loginFormModel.loginButtonLoading"
      >
        {{ loginFormModel.loginButtonName }}
      </el-button>
    </div>
  </el-form>
</template>

<style lang="scss" scoped></style>
